package com.yzh.homework;

import java.io.*;

/**
 * @author 于振晗
 * @title: CopyDirectory
 * @description: TODO 复制目录
 * @date 2020年09月07日
 * @path com.yzh.homework
 */
public class CopyDirectory02 {

    public static void main(String[] args) {
        copyDir("D:\\sxtjava\\test","D:\\sxtjava\\test1");
    }


    public static void copyDir(String source,String destDir){
        //源文件
        File dir1 = new File(source);
        if(!dir1.exists()){
            System.out.println("源文件不存在");
            return;
        }

        //目标文件
        File dir2 = new File(destDir);
        if(!dir2.exists()){
            dir2.mkdirs();
        }
        File[] files = dir1.listFiles();
        for (File file : files) {
            if(file.isFile()){
                copyData(source+"/"+file.getName(),destDir+"/"+file.getName());
            }else{
                copyDir(source+"/"+file.getName(),destDir+"/"+file.getName());
            }
        }

    }

    public static void copyData(String path, String copyPath){
        try(BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path));
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(copyPath));) {
            byte [] byt = new byte[1024];
            int len = 0;
            while ((len = bis.read(byt)) != -1) {
                bos.write(byt,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
